<template>
  <div>
    <el-button type="primary" @click="add">添加分类</el-button>
    <!--添加模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
        <el-cascader
          v-model="result"
          :options="categorylist"
          :props="{ label: 'cat_name', value: 'cat_id', checkStrictly: true }"
          clearable
        ></el-cascader>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table
      :data="categorylist"
      style="width: 100%; margin-bottom: 20px"
      row-key="cat_id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column type="index" label="#" sortable> </el-table-column>
      <el-table-column prop="cat_name" label="分类名称" sortable>
      </el-table-column>
      <el-table-column prop="cat_deleted" label="是否有效">
        <template slot-scope="scope">
          <el-button
            type="success"
            icon="el-icon-check"
            circle
            v-if="scope.row.cat_deleted == false"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-close"
            circle
            v-if="scope.row.cat_deleted == true"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column prop="cat_level" label="排序">
        <template slot-scope="scope">
          <el-button type="primary" plain v-if="scope.row.cat_level == 0"
            >一级</el-button
          >
          <el-button type="success" plain v-if="scope.row.cat_level == 1"
            >二级</el-button
          >
          <el-button type="danger" plain v-if="scope.row.cat_level == 2"
            >三级</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 编辑弹出层 -->
    <el-dialog title="提示" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="ruleForm1"
        :rules="rules"
        ref="ruleForm1"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm1.cat_name"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="edit">确定修改</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { categorys, delcate, editcategory, addcategory } from "../utils/api";
export default {
  data() {
    return {
      result: [],
      list: [], //数据
      ruleForm: {}, //添加分类数据
      ruleForm1: {}, //编辑分类数据
      dialogVisible: false, //添加模态框
      dialogVisible1: false, //编辑模态框
      categorylist: [], //数据列表
      params: {
        //分页
        type: [],
        pagenum: 1,
        pagesize: 10,
      },
      total: 0, //条数
      rules: {
        name: [
          { required: true, message: "分类名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      this.params.pagesize = val;
      this.getLists();
    },
    handleCurrentChange(val) {
      this.params.pagenum = val;
      this.getLists();
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.dialogVisible1 = true;
      this.ruleForm1 = { ...row };
    },
    // 确定修改
    edit() {
      editcategory({
        id: this.ruleForm1.cat_id,
        cat_name: this.ruleForm1.cat_name,
      }).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            message: "恭喜你,修改成功",
            type: "success",
          });
        }
        this.getLists();
        this.dialogVisible1 = false;
      });
    },
    //删除
    handleDelete(row) {
      console.log(row);
      delcate(row.cat_id).then((res) => {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
            this.getLists();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    //数据
    getLists() {
      categorys(this.params).then((res) => {
        console.log(res);
        this.categorylist = res.data.data.result;
        this.total = res.data.data.total;
      });
    },
    // 添加
    add() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.result);
          if (this.result.length == 0) {
            this.ruleForm.cat_pid = 0;
            this.ruleForm.cat_level = 0;
          } else {
            this.ruleForm.cat_pid = this.result[this.result.length - 1]; //列表的cat_pid等于数组长度-1
            this.ruleForm.cat_level = this.result.length;
          }
          console.log(this.ruleForm);
          addcategory(this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 201) {
              alert("添加成功");
              this.getLists();
              this.dialogVisible = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.getLists();
  },
  mounted() {},
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped></style>
